package 原型模式.客户类;

import java.io.*;

public class Customer implements Cloneable, Serializable {
    private Address address;
    public Customer(Address address) {
        this.address = address;
    }

    //深拷贝（用对象流的方式进行拷贝）
    public Customer deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(bao);
        oos.writeObject(this);//将当前对象写入对象流

        //从对象流中取出
        ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());
        ObjectInputStream ois=new ObjectInputStream(bis);
        return (Customer) ois.readObject();
    }


    //浅拷贝
    @Override
    protected Customer clone() throws CloneNotSupportedException {
        return (Customer)super.clone();
    }

    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "address=" + address +
                '}';
    }
}
